<template>
  <!-- 我的审批 -->
  <div>
    <div style="display: flex; justify-content: flex-end">
      <el-select
        v-model="value"
        placeholder="请选择"
        @change="changeSelect"
      >
        <el-option
          v-for="item in options"
          :key="item.value"
          :label="item.label"
          :value="item.value"
        >
        </el-option>
      </el-select>
      <el-input
        placeholder="请输入需求资源名称"
        v-model="name"
        class="input-with-select"
        style="width: 300px"
        clearable
      >
        <el-button
          slot="append"
          icon="el-icon-search"
          @click="searchBtn"
        ></el-button>
      </el-input>
    </div>

    <el-table
      :data="tableData"
      border
      style="width: 100%; margin: 10px 0"
      height="560"
    >
      <el-table-column
        fixed
        prop="dataName"
        label="需求资源名称"
        width="200"
        align="center"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        prop="descri"
        label="预期成效"
        width="120"
        align="center"
        :show-overflow-tooltip="true"
      >
      </el-table-column>
      <el-table-column
        prop="sqr"
        label="申请人"
        width="120"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="createTime"
        label="申请时间"
        align="center"
        width="200"
      >
        <template slot-scope="scope">
          {{ scope.row.createTime | formatDate }}
        </template>
      </el-table-column>
      <el-table-column
        prop="spzt"
        label="审批状态"
        width="120"
        align="center"
      >
        <template slot-scope="scope">
          <span v-if="scope.row.spzt === 0">待数据局初审</span>
          <span v-else-if="scope.row.spzt === 1">初审通过，待数据源部门审核 </span>
          <span v-else-if="scope.row.spzt === 2">初审通过且已有数据编目</span>
          <span v-else-if="scope.row.spzt === 3">初审退回</span>
          <span v-else-if="scope.row.spzt === 4">待数据局复审</span>
          <span v-else-if="scope.row.spzt === 5">数据源部门退回</span>
          <span v-else-if="scope.row.spzt === 6">数据局复审退回</span>
          <span v-else-if="scope.row.spzt === 7">复审通过</span>
        </template>
      </el-table-column>
      <el-table-column
        prop="spr"
        label="数据局审核人"
        width="120"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="dataDeptSpr"
        label="数据源部门审核人"
        width="120"
        align="center"
      >
      </el-table-column>
      <el-table-column
        prop="auditTime"
        label="审核时间"
        width="200"
        align="center"
      >
        <template slot-scope="scope" v-if="scope.row.auditTime && scope.row.auditTime !== null">
          {{ scope.row.auditTime | formatDate }}
        </template>
      </el-table-column>
      <el-table-column
        fixed="right"
        align="center"
        label="操作"
        width="120">
        <template slot-scope="scope">
          <el-button @click="handleClickDetail(scope.row)" type="text" size="small">查看</el-button>
          <el-button v-if="scope.row.spzt === 1" @click="spBtn(scope.row)" type="text" size="small">审批</el-button>
          <el-button v-if="scope.row.spzt === 3 || scope.row.spzt === 5 || scope.row.spzt === 6" @click="cxtjBtn(scope.row)" type="text" size="small">重新审批</el-button>
        </template>
      </el-table-column>
    </el-table>
    <div style="display: flex; justify-content: flex-end">
      <el-pagination
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="currentPage"
        :page-size="pageSize"
        :page-sizes="[10, 20, 50, 100]"
        layout="prev, pager, next, sizes, jumper"
        :total="totalNum"
      >
      </el-pagination>
    </div>
    <!--        详情弹窗-->
    <el-dialog
      :visible.sync="dialogVisible"
      width="80%"
    >
      <el-descriptions title="申请详情">
        <el-descriptions-item label="需求资源名称">{{detailList.dataName}}</el-descriptions-item>
        <el-descriptions-item label="服务方式">
          {{detailList.type === 0
          ? '数据库'
          : detailList.type === 1
            ? 'API 接口'
            : detailList.type === 2
              ? '文件'
              : '未知类型'}}
        </el-descriptions-item>
        <el-descriptions-item label="数据项">{{ detailList.dataItem }}</el-descriptions-item>
        <el-descriptions-item label="资源覆盖范围">{{detailList.resourceRange}}</el-descriptions-item>
        <el-descriptions-item label="申请人名称">{{detailList.sqr}}</el-descriptions-item>
        <el-descriptions-item label="申请人手机号">{{detailList.phone}}</el-descriptions-item>
        <el-descriptions-item label="申请人邮箱">{{detailList.email}}</el-descriptions-item>
        <el-descriptions-item label="数据需求部门">{{detailList.appDept}}</el-descriptions-item>
        <el-descriptions-item label="需求描述">{{detailList.descri}}</el-descriptions-item>
        <el-descriptions-item label="使用起始时间">{{detailList.startTime | formatDate}}</el-descriptions-item>
        <el-descriptions-item label="使用结束时间">{{detailList.endTime | formatDate}}</el-descriptions-item>
        <el-descriptions-item label="应用场景">{{detailList.appScene}}</el-descriptions-item>
<!--        <el-descriptions-item label="出参">{{detailList.inputArg ? detailList.inputArg : '无'}}</el-descriptions-item>-->
<!--        <el-descriptions-item label="入参">{{detailList.outputArg ? detailList.outputArg : '无'}}</el-descriptions-item>-->
        <el-descriptions-item label="数据所属部门">{{detailList.dataDept}}</el-descriptions-item>

      </el-descriptions>
      <span slot="footer" class="dialog-footer">
    <el-button @click="dialogVisible = false">取 消</el-button>
    <el-button type="primary" @click="dialogVisible = false">确 定</el-button>
  </span>
    </el-dialog>
<!--    审批弹窗-->
    <el-dialog
      title="审批"
      :visible.sync="dialogVisibleSP"
      width="50%">
      <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="审批结果" prop="spzt">
          <el-radio-group v-model="ruleForm.spzt">
            <el-radio :label="0">通过</el-radio>
            <el-radio :label="1">退回</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="审批意见" prop="auditReply">
          <el-input
            type="textarea"
            :autosize="{ minRows: 3, maxRows: 4}"
            placeholder="请输入审批意见"
            v-model="ruleForm.auditReply">
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="addSpBtn('ruleForm')">确定</el-button>
          <el-button @click="resetForm('ruleForm')">重置</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
<!--    重新审批-->
    <el-dialog
      title="重新审批"
      :visible.sync="dialogVisibleCxtj"
      width="60%">
      <el-form :model="ruleFormCxtj" :rules="rulesCxtj" ref="ruleFormCxtj" label-width="100px" class="demo-ruleForm">
        <el-form-item label="审批结果" prop="spzt">
          <el-radio-group v-model="ruleFormCxtj.spzt">
            <el-radio :label="0">重新提交</el-radio>
            <el-radio :label="1">退回</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="审批意见" prop="auditReply">
          <el-input
            type="textarea"
            :autosize="{ minRows: 3, maxRows: 4}"
            placeholder="请输入审批意见"
            v-model="ruleFormCxtj.auditReply">
          </el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="addCxtjBtn('ruleFormCxtj')">确定</el-button>
          <el-button @click="resetFormCxtj('ruleFormCxtj')">重置</el-button>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
  </span>
    </el-dialog>
  </div>
</template>
<script>
import {
  geGetbydeptspr,
  xqlistDetail,
  cxDeletebyid,
  getAuditdemand,
  gecxtj,
} from "@/utils/httpUtils";
import Treeselect from "@riophae/vue-treeselect";
export default {
  components: {Treeselect},
  data() {
    return {
      cxtjId:'',
      dialogVisibleCxtj:false,
      spId:'',
      ruleFormCxtj:{
        spzt:'',
        auditReply:''
      },
      ruleForm: {
        spzt:'',
        auditReply:''
      },
      rulesCxtj:{
        spzt:[
          { required: true, message: '请选择审批状态', trigger: 'change' }
        ],
        auditReply: [
          { required: true, message: '请输入审批意见', trigger: 'blur' },
        ],
      },
      rules: {
        spzt: [
          { required: true, message: '请选择审批状态', trigger: 'change' }
        ],
        auditReply: [
          { required: true, message: '请输入审批意见', trigger: 'blur' },
        ],
      },
      formLabelWidth: '120px',
      dialogVisibleSP:false,
      dialogVisible: false,
      show: false,
      value: "",
      options: [
        {
          value: "",
          label: "全部",
        },
        {
          value: "0",
          label: "待数据局初审",
        },
        {
          value: "1",
          label: "初审通过，待数据源部门审核",
        },
        {
          value: "2",
          label: "初审通过且已有数据编目",
        },
        {
          value: "3",
          label: "初审退回",
        },
        {
          value: "4",
          label: "待数据局复审",
        },
        {
          value: "5",
          label: "数据源部门退回",
        },
        {
          value: "6",
          label: "数据局复审退回",
        },
        {
          value: "7",
          label: "复审通过",
        },
      ],
      name: "",
      tableData: [],
      activeName: "dataResource",
      currentPage: 1,
      pageNo: 1,
      pageSize: 10,
      status: "",
      totalNum: 0,
      detailsList: {
        resourceName: "",
        resourceType: "",
        applyInstitution: "",
        createUser: "",
        type: "",
        updateTime: "",
        description: "",
        createTime: "",
        updateUser: "",
        auditStatus: "",
      },
      updateCycleList: [],
      shareTypeList: [],
      ShareModeList: [],
      resourceType: 0,
      detailList:{}
    };
  },
  mounted() {
    this.initData()
  },
  //   处理1703592547000 时间格式
  filters: {
    formatDate(timestamp) {
      const date = new Date(timestamp);
      const year = date.getFullYear();
      const month = String(date.getMonth() + 1).padStart(2, "0");
      const day = String(date.getDate()).padStart(2, "0");
      const hours = String(date.getHours()).padStart(2, "0");
      const minutes = String(date.getMinutes()).padStart(2, "0");
      const seconds = String(date.getSeconds()).padStart(2, "0");

      return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
    },
  },
  methods: {
    cxtjBtn(row){
      console.log(row.id)
      this.cxtjId = row.id
      this.dialogVisibleCxtj = true
      gecxtj({id:row.id}).then(res=>{
        console.log(res,"重新提交")
        this.ruleFormCxtj.auditReply = res.dataDeptSpyj
      })
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    addCxtjBtn(formName){
      this.$refs[formName].validate(valid=> {
        if(valid){
          const formattedRuleForm = {
            ...this.ruleFormCxtj,
            id:this.cxtjId,
          };
          getAuditdemand(formattedRuleForm).then(res=>{
            console.log(res,"重新提交111")
            this.$message({
              message: res,
              type: 'success'
            });
            this.initData()
            this.resetFormCxtj('ruleFormCxtj')
            this.dialogVisibleCxtj = false
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      })
    },
    resetFormCxtj(formName){
      this.$refs[formName].resetFields();
    },
    addSpBtn(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          // alert('submit!');
          const formattedRuleForm = {
            ...this.ruleForm,
            id:this.spId
          };
          getAuditdemand(formattedRuleForm).then(res=>{
            console.log(res,"新增123")
            this.$message({
              message: res,
              type: 'success'
            });
            // 关闭弹窗
            this.dialogVisibleSP = false
            // 清表单内容
            this.resetForm('ruleForm')
            //   重新渲染
            this.initData()
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });
    },
    spBtn(row){
      this.dialogVisibleSP = true
      this.spId = row.id
    },
    // cxBtn(row){
    //   this.$confirm('是否撤回?', '提示', {
    //     confirmButtonText: '确定',
    //     cancelButtonText: '取消',
    //     type: 'warning'
    //   }).then(() => {
    //     cxDeletebyid({id:row.id}).then(res=>{
    //       console.log(res,"撤销123")
    //       if(res.code == 200){
    //         this.$message({
    //           type: 'success',
    //           message: '撤回成功!'
    //         });
    //         this.initData()
    //       }
    //     }).catch((err)=>{
    //       console.log(err,"err")
    //       this.$message({
    //         type: 'error',
    //         message: err.response.data.message
    //       });
    //     })
    //   }).catch(() => {
    //     this.$message({
    //       type: 'info',
    //       message: '已取消'
    //     });
    //   })
    //
    // },
    handleClickDetail(row){
      console.log(row.id, "row")
      xqlistDetail({id:row.id}).then(res=>{
        console.log(res,'详情')
        this.dialogVisible = true
        this.detailList = res
      })
    },
    searchBtn() {
      this.initData();
    },

    changeSelect(val) {
      console.log(val, "val");
      this.status = val;
      this.initData();
    },
    // 初始化数据
    initData() {
      geGetbydeptspr({
        pageNum: this.pageNo,
        pageSize: this.pageSize,
        dataName: this.name,
        spzt:this.value,
      }).then((res) => {
        console.log(res, "我的申请，数据资源0");
        this.tableData = res.content;
        this.totalNum = res.totalElements;
        this.name = "";
      });
    },

    handleSizeChange(val) {
      console.log(`每页 ${val} 条`);
      this.pageSize = val;
      this.initData();
    },
    handleCurrentChange(val) {
      console.log(`当前页: ${val}`);
      this.pageNo = val;
      this.initData();
    },
  },
};
</script>
<style  scoped>
.listToggle {
  padding: 13px 20px;
}

.listToggle.backBtn {
  border-bottom: 4px solid #198ef8;
}
</style>



